﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Windows.Forms;
using System.Xml.Linq;
using System.IO;

namespace UavForm
{
    public partial class Normal : Form
    {
        public Normal()
        {
            InitializeComponent();
        }

        public DbHelper dbHelper;

        private void Normal_Load(object sender, EventArgs e)
        {
            dbHelper = new DbHelper();  
            comboBox1.Items.Clear();
            var days = dbHelper.GetDistinctDays();
            foreach (var day in days)
            {
                comboBox1.Items.Add(day);  
            }
            Report();
        }

        public void Report() {
            try
            {
                var daylist = dbHelper.QueryDays("SELECT DISTINCT day FROM UAAVData WHERE day NOT IN( SELECT DISTINCT day FROM UAAVData WHERE speed = 0)");
                var errorDay = dbHelper.QueryDays("SELECT DISTINCT day FROM UAAVData WHERE speed = 0");
                var otherDay = dbHelper.QueryDays("SELECT DISTINCT day FROM UAAVData WHERE CabTemp>30 and day not in(select DISTINCT day from uaavdata where speed=0)");

                textBox2.Text = $"Success Days:\r {string.Join(",", daylist)}\r\n\r\n" +
                          $"Fail Days:\r {string.Join(",", errorDay)}\r\n\r\n" +
                          $"Succeed but fail Days:\r {string.Join(",", otherDay)}\n";
            }
            catch (Exception ex)
            {
                MessageBox.Show($"Operation Failed: {ex.Message}");
            }
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            if (comboBox1.Text == "")
            {
                MessageBox.Show("Please select day", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (comboBox2.SelectedIndex == -1)
            {
                MessageBox.Show("Please select condition", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            try
            {
                string sql = "";
                switch (comboBox2.SelectedIndex)
                {
                    case 0:
                        sql = "SELECT  MAX(speed) AS MaxSpeed, AVG(speed) AS AvgSpeed,"+
                            "MAX(cabtemp) AS MaxCabTemp, AVG(cabtemp) AS AvgCabTemp," +
                            "AVG(engtemp) AS AvgEngTemp, MAX(engtemp) AS MaxEngTemp," +
                            "MAX(shockwear) AS MaxShockWear, AVG(shockwear) AS AvgShockWear" +
                            $" FROM uaavdata WHERE day = {comboBox1.Text}";
                        break;
                    case 1:
                        sql = $"SELECT day, MIN(fuel) AS MinFuel, MIN(battery) AS MinBattery FROM uaavdata WHERE day = {comboBox1.Text}";
                        break;
                    case 2:
                        sql = $"SELECT *  FROM uaavdata  WHERE day IN (SELECT DISTINCT day FROM uaavdata WHERE speed = 0) AND day = {comboBox1.Text}";
                        break;
                }
                var uavData = dbHelper.FetchData(sql);
                SelectData.DataSource = uavData;
                SelectData.DataMember = uavData.Tables[0].TableName;
                SelectData.RowHeadersVisible = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show($"An Error Occurred: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
   

    }
}

